﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Roman Ranzmaier (http://www.ranzmaier.at/contact.aspx).  All rights reserved.
// 
// Codeplex project: http://gadgeteernetworking.codeplex.com
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;


using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using System.Collections;
using Ranzmaier.NETMF.GadgeteerNetworking;

namespace Sample
{


   
    public partial class Program
    {

        private Queue logEntries;
        private byte logRows;
        private Font font;


        void ProgramStarted()
        {
            
            
            int tcpPort = 5000; //Port used for TCP communication
            int udpPort = 5001; // Port used for UDP communication

            Debug.Print("Program Started");
            //set your Network settings here
            ethernet.UseStaticIP("10.0.0.225", "255.255.255.0", "10.0.0.138");

            #region Initialize some variables
            font = Resources.GetFont(Resources.FontResources.small);
            logRows = (byte)(display.Height / font.Height);
            logEntries = new Queue();
            display.SimpleGraphics.AutoRedraw = false;
            display.SimpleGraphics.Clear();
            #endregion
           
            //Initialize server
            var udpServer = ethernet.CreateUDPServer(udpPort);
            udpServer.DataReceived +=new UdpServer.DataReceivedEventHandler(udpServer_DataReceived);

            var tcpServer = ethernet.CreateTCPServer(tcpPort);
            tcpServer.DataReceived += new TcpServer.DataReceivedEventHandler(tcpServer_DataReceived);
            
            //Start server
            udpServer.Start();
            tcpServer.Start();
            Debug.Print("Server Started");
 
        }


        private void tcpServer_DataReceived(System.Net.Sockets.Socket sender, TcpServer.DataReceiveEventArgs e)
        {
            int command = e.Reader.ReadInt32(); // First 32 bits for command

            switch (command)
            {
                case 1: var text = e.Reader.ReadString();
                    Log(text);
                    break;
                case 2:
                    byte red = e.Reader.ReadByte();
                    byte green = e.Reader.ReadByte();
                    byte blue = e.Reader.ReadByte();
                    display.SimpleGraphics.BackgroundColor = GT.Color.FromRGB(red, green, blue);
                    display.SimpleGraphics.Redraw();
                    break;
            }

        }



        private void udpServer_DataReceived(System.Net.EndPoint sender, UdpServer.DataReceiveEventArgs e)
        {
            Log(new string(System.Text.Encoding.UTF8.GetChars(e.Data)));

        }


        private void Log(string p)
        {
            logEntries.Enqueue(p);
            display.SimpleGraphics.Clear();
            var l = logEntries.ToArray();
            for (int i = 0; i < l.Length; i++)
                display.SimpleGraphics.DisplayText(l[i].ToString(), font, GT.Color.Orange, 1, (uint)(font.Height * (i)));
            while (logEntries.Count > logRows)
                logEntries.Dequeue();
            display.SimpleGraphics.Redraw();
        }
    }
}
